{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lC9vRGdIPQwF"
      },
      "source": [
        "更新于北京时间2025年2月19日23:00\n",
        "\n",
        "说明：\n",
        "\n",
        "1.此程序提供TensorRT与CUDA引擎。\n",
        "\n",
        "使用时可参考本人在B站发的视频：\n",
        "\n",
        "https://www.bilibili.com/video/BV1bn6tYeE4B/?share_source=copy_web&vd_source=bad2ceb2f52b7f81a9436e832aadda15\n",
        "\n",
        "2.使用之前先选择使用T4 GPU。如使用TensorRT后端会有额外约两分钟的配置环境时间，请耐心等待。（TensorRT）已为28b与HumanSL提供timing cache，连接即用。线程已调到最佳，无需修改。\n",
        "\n",
        "3.连接的命令行为：\n",
        "\n",
        "ikatago.exe -p all -u 用户名 --password 密码\n",
        "\n",
        "注意这里用的platform不是colab。\n",
        "\n",
        "4.配置环境后包中有28b 18b 40b 30b 20b权，可以在命令行中使用--kata-weight 指定。以使用18b为例：\n",
        "\n",
        "ikatago.exe -p all -u 用户名 --password --kata-weight 18b\n",
        "\n",
        "5.需要使用自己的权重就在下面一个单元格最后一行的wget后面加上权重下载链接，但权重必须以.bin.gz结尾\n",
        "\n",
        "6.支持humansl，但只能用28b权重带。8D以下命令行为：（以8段水平为例）\n",
        "\n",
        "ikatago.exe -p all -u 用户名 --password 密码 --kata-name humansl8d\n",
        "\n",
        "启动引擎后可在gtp控制区输入kata-set-param humanSLProfile rank_8d，将8d改为需要的等级即可，例如20k、5k、1d、5d等\n",
        "\n",
        "9d的命令行这样写：\n",
        "\n",
        "ikatago.exe -p all -u 用户名 --password 密码 --kata-name humansl9d"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bQu7Y_Wo9_BI"
      },
      "outputs": [],
      "source": [
        "# 配置环境（务必启动这个单元格！等运行完毕再点下面的单元格）\n",
        "\n",
        "backend = 'cuda' # 引擎后端，可填trt（tensorrt）或cuda\n",
        "backend = backend.lower()\n",
        "if backend == 'trt' or backend == 'tensorrt' or backend == 'cuda':\n",
        "  pass\n",
        "else:\n",
        "  raise ValueError('参数backend值无效，请检查输入。')\n",
        "\n",
        "import requests\n",
        "downloadURL='https://github.com/FengmingGo/katago_for_colaboratory/releases/' # 后面除了katago权重、libzip之外其他所有文件都在此目录下，Ctrl+单击可进入。\n",
        "\n",
        "# 装libssl\n",
        "%cd ~\n",
        "!wget {downloadURL}download/20241228/libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "!dpkg -i libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb && rm libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "# 装libzip\n",
        "!wget https://libzip.org/download/libzip-1.11.2.tar.gz\n",
        "!tar -zxvf libzip-1.11.2.tar.gz\n",
        "%cd libzip-1.11.2\n",
        "!cmake . && make && make install\n",
        "!ldconfig /usr/local/lib && ldconfig /usr/local/lib64\n",
        "%cd ..\n",
        "!rm -rf libzip-1.11.2 && rm libzip-1.11.2.tar.gz\n",
        "\n",
        "if backend == 'cuda': # 如选用CUDA后端\n",
        "  !wget {downloadURL}download/20250202/cuda.zip\n",
        "  !unzip cuda && rm cuda.zip\n",
        "else: # 如选用TensorRT后端\n",
        "  # 装TensorRT\n",
        "  !apt-get install libnvinfer10=10.8.0.43-1+cuda12.8\n",
        "  # 下载ikatago相关文件、timing cache\n",
        "  !wget {downloadURL}download/20250219/tensorrt.zip\n",
        "  !unzip tensorrt && rm tensorrt.zip\n",
        "  !mkdir -p /root/.katago/trtcache/\n",
        "  %cd /root/.katago/trtcache/\n",
        "  !wget {downloadURL}download/20250219/trt-100800_gpu-131365bf_tune-1bf250279187_exact19x19_batch12_fp16\n",
        "  !wget {downloadURL}download/20250219/trt-100800_gpu-131365bf_tune-1bf250279187_exact19x19_batch8_fp16\n",
        "  !wget {downloadURL}download/20250219/trt-100800_gpu-131365bf_tune-460539d595d8_exact19x19_batch8_fp16\n",
        "\n",
        "%cd ~\n",
        "!chmod -R 777 ./work/\n",
        "strongest_weight = requests.get(\"https://katagotraining.org/api/networks/get_strongest/?format=json\").text.split(\"\\\"name\\\":\")[1].split(\",\\\"created_at\\\"\")[0] # 获取当前最强权重的名字\n",
        "!wget https://github.com/lightvector/KataGo/releases/download/v1.15.0/b18c384nbt-humanv0.bin.gz -O ./work/data/humansl.bin.gz\n",
        "!wget https://media.katagotraining.org/uploaded/networks/models/kata1/{strongest_weight}.bin.gz -O ./work/data/28b.bin.gz\n",
        "!wget https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b18c384nbt-s9996604416-d4316597426.bin.gz -O ./work/data/18b.bin.gz\n",
        "!wget https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b40c256-s11840935168-d2898845681.bin.gz -O ./work/data/40b.bin.gz\n",
        "!wget https://github.com/lightvector/KataGo/releases/download/v1.4.5/g170-b30c320x2-s4824661760-d1229536699.bin.gz -O ./work/data/30b.bin.gz\n",
        "!wget https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b20c256x2-s5303129600-d1228401921.bin.gz -O ./work/data/20b.bin.gz\n",
        "!wget\n",
        "print('配置环境完毕，已下载当前最强权重：'+strongest_weight)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 启动ikatago server\n",
        "username=\"ikatago\" # 将双引号中的ikatago修改为自己需要的ikatago用户名\n",
        "password=\"kinfkong\" # 将双引号中的kinfkong修改为自己需要的ikatago连接密码\n",
        "%cd ~/work\n",
        "!./run.sh {username} {password}"
      ],
      "metadata": {
        "id": "PnyBm7wHLUiK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!lsb_release -a # 获取系统版本等信息\n",
        "!nvidia-smi # 获取GPU及其利用率等信息\n",
        "!nvcc -V # 查询CUDA版本等信息"
      ],
      "metadata": {
        "id": "9jEIyYLWTPjY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# benchmark，前提是在运行完第一个单元格后进行\n",
        "model='28b' # 默认可填18b、28b、40b、30b、20b\n",
        "visits=3000 # 每个position用这么多计算量\n",
        "%cd ~/work/data\n",
        "!./katago benchmark -model {model}.bin.gz -v {visits}"
      ],
      "metadata": {
        "id": "uvGuCyAyS6u7"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}